from django.db import models


# Create your models here.


class FileInfo(models.Model):
    pName = models.CharField(max_length=20, verbose_name="文件名称", db_index=True, editable=False)
    pUpdate = models.DateField(verbose_name="上传日期", auto_created=True)
    pImage = models.ImageField(upload_to='userUpImage/', null=True, blank=True, verbose_name='文件路径')
    pSize = models.PositiveIntegerField(verbose_name="文件大小(kb)", null=True, blank=True, editable=False)

    class Meta:
        db_table = 'file_info'
        ordering = ['pName', 'pSize', 'pUpdate']

    def __str__(self):
        return self.pName

    def save(self, *args, **kwargs):
        if self.pImage:
            self.pSize = self.pImage.size  # 获取文件大小
            self.pName = self.pImage.name
        super().save(*args, **kwargs)
